import json
import requests
import unittest
from parameterized import parameterized
from api.longin import LoginAPI
import config
from utils import common_assert


def BuildData():
    testdata = []
    data_file = config.BASE_DIR + "/data/params.json"
    with open(data_file, encoding="utf-8") as f:
        receivedata = json.load(f)
        for i in receivedata:
            param = i.get("param")
            testexpect = i.get("testexpect")
            testdata.append((param, testexpect))
    print(testdata)
    return testdata


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.session = requests.session()

    def tearDown(self):
        self.session.close()

    @parameterized.expand(BuildData())
    def test01_login(self, param, testexpect):
        response = LoginAPI().kklLogin(self.session, param)
        response_json = response.json()
        config.prints(response)
        print(f'响应体：{response_json}')
        common_assert(self,response_json,testexpect)
        try:
            res_token = response_json.get("data").get("resultObject").get("token")
            print(f"令牌：{res_token}")
        # 写入token到config.TOKEN_RESULT
            if res_token:
                config.TOKEN_RESULT[0]["token"]= res_token
                print(config.TOKEN_RESULT)
            else:
                print("未能提取令牌")
        except Exception as e:
            print(f'error:{e}')


